package com.koron.order.maintain.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.maintain.bean.entity.MaintainItemParamsPo;
import com.koron.order.maintain.mapper.MaintainItemParamsMapper;
import com.koron.order.maintain.service.MaintainItemParamsService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (MaintainItemParams)表服务实现类
 *
 * @author zhouj
 * @since 2021-09-23 16:00:01
 */
@Service("maintainItemParamsService")
public class MaintainItemParamsServiceImpl implements MaintainItemParamsService {

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, MaintainItemParamsPo maintainItemParams) {

        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);

        return mapper.insert(maintainItemParams);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<MaintainItemParamsPo> maintainItemParams) {

        if (CollectionUtils.isEmpty(maintainItemParams)) {
            return 0;
        }
        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);
        //mapper.deleteByMaintainEquipmentId(maintainItemParams.get(0).getMaintainEquipId());
        mapper.deleteByItemId(maintainItemParams.get(0).getItemId());
        maintainItemParams.forEach(p -> {
            if (StringUtils.isEmpty(p.getId())) {
                p.setId(CodeTools.getCode32());
            }
        });

        return mapper.insertBatch(maintainItemParams);
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<MaintainItemParamsPo> maintainItemParams) {

        if (CollectionUtils.isEmpty(maintainItemParams)) {
            return 0;
        }
        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);

        return mapper.insertOrUpdateBatch(maintainItemParams);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, MaintainItemParamsPo maintainItemParams) {
        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);
        return mapper.update(maintainItemParams);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("deleteByItemId")
    public int deleteByItemId(SessionFactory factory, String itemId) {

        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);
        return mapper.deleteByItemId(itemId);
    }

    @Override
    @TaskAnnotation("queryById")
    public MaintainItemParamsPo queryById(SessionFactory factory, String id) {

        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<MaintainItemParamsPo> queryList(SessionFactory factory, MaintainItemParamsPo maintainItemParams) {

        MaintainItemParamsMapper mapper = factory.getMapper(MaintainItemParamsMapper.class);

        List<MaintainItemParamsPo> list = mapper.queryList(maintainItemParams);

        return list;
    }

}